/*
 * UAE - The Un*x Amiga Emulator
 *
 * Memory access functions
 *
 * Copyright 1996 Bernd Schmidt
 */

#pragma once

#include "sysconfig.h"

#define ALIGN_POINTER_TO32(p) ((~(uint)(p)) & 3)

static __forceinline uint do_get_mem_long(uint* a)
{
    #ifdef MEMSTAT
    MemStat::LongReads++;
    #endif

    // #if !defined (X86_MSVC_ASSEMBLY_MEMACCESS)
    byte* b = (byte*)a;
    return (*b << 24) | (*(b + 1) << 16) | (*(b + 2) << 8) | (*(b + 3));
    // #else
    // uint retval;
    // __asm
    // {
    //     mov eax, a
    //     mov ebx, [eax]
    //     bswap ebx
    //     mov retval, ebx
    // }
    // return retval;
    // #endif
}

static __forceinline ushort do_get_mem_word(ushort* a)
{
    #ifdef MEMSTAT
    MemStat::WordReads++;
    #endif

    byte* b = (byte*)a;

    return (*b << 8) | (*(b + 1));
}

#define do_get_mem_byte(a) ((uint) * (byte*)(a))

static __forceinline void do_put_mem_long(uint* a, uint v)
{
    #ifdef MEMSTAT
    MemStat::LongWrites++;
    #endif

    // #if !defined (X86_MSVC_ASSEMBLY_MEMACCESS)
    byte* b = (byte*)a;

    *b = v >> 24;
    *(b + 1) = (byte)(v >> 16);
    *(b + 2) = (byte)(v >> 8);
    *(b + 3) = (byte)v;
    // #else
    // __asm
    // {
    //     mov eax, a
    //     mov ebx, v
    //     bswap ebx
    //     mov[eax], ebx
    // }
    // #endif
}

static __forceinline void do_put_mem_word(ushort* a, ushort v)
{
    #ifdef MEMSTAT
    MemStat::WordWrites++;
    #endif

    byte* b = (byte*)a;

    *b = v >> 8;
    *(b + 1) = (byte)v;
}

static __forceinline void do_put_mem_byte(byte* a, byte v)
{
    *a = v;
}

#define call_mem_get_func(func, addr) ((*func)(addr))
#define call_mem_put_func(func, addr, v) ((*func)(addr, v))